package br.uvv.sao.managedBean;

import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletResponse;

import br.uvv.sao.domain.AlunoOferta;
import br.uvv.sao.domain.Disciplina;
import br.uvv.sao.domain.OfertaDisciplina;
import br.uvv.sao.domain.Professor;
import br.uvv.sao.domain.Prova;
import br.uvv.sao.rn.RelatorioRN;
import br.uvv.sao.util.MsgUtil;
import br.uvv.sao.util.PdfUvv;
import br.uvv.sao.util.RNException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class RelatorioBean implements Serializable {
	
	private static final long serialVersionUID = -8226675396008736540L;
	
	private RelatorioRN relatorioRN = new RelatorioRN();
	private OfertaDisciplina ofertaInstance;
	private OfertaDisciplina ofertaInstanceTemp;
	private List<OfertaDisciplina> ofertaList;
	private List<AlunoOferta> alunoOfertaList;
	
	//Prompt Busca Oferta Disciplina
	private List<Disciplina> disciplinaList;
	private Long idDisciplina;
	private List<Professor> professorList;
	private Long idProfessor;
	private String buscaSemestre;
	private String buscaAno;
	private List<SelectItem> anosList;
	private Prova provaInstance;
	private List<Prova> provaList;
	
	private Date diaHoje;
	
	public String goRelatorios(){
		ofertaInstance = new OfertaDisciplina();
		ofertaInstanceTemp = new OfertaDisciplina();
		ofertaList = new ArrayList<OfertaDisciplina>();
		professorList = relatorioRN.listarProfessor();
		try {
			disciplinaList = relatorioRN.listarDisciplina();
		} catch (RNException e) {
			e.printStackTrace();
		}
		return "/views/relatorios/show.xhtml";
	}
	
	public String goHome(){
		ofertaInstance = null;
		ofertaInstanceTemp = null;
		ofertaList = null;
		professorList = null;
		disciplinaList = null;
		return "goHome";
	}
	
	public String imprimir(){
		try {
			HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
			response.setContentType("application/pdf");
	        response.setHeader("Content-disposition", "attachment;filename=RelatorioProva.pdf");
	
	        Document doc = new Document(PageSize.A4);
			doc.setMargins(30, 30, 60, 30);
	        
	        PdfUvv pdf = new PdfUvv();
	        pdf.setTituloRelatorio("Título do Ralatório");
	        
	        PdfWriter pdfWriter = PdfWriter.getInstance(doc, response.getOutputStream());
	        pdfWriter.setPageEvent(pdf);
	        
	        doc.open();
	        
	        PdfPTable content = new PdfPTable(100);
	        content.setWidthPercentage(100);
	        PdfPCell clienteNome = new PdfPCell(new Paragraph("Testando"));
	  		clienteNome.setColspan(100);
	  		clienteNome.setHorizontalAlignment(Element.ALIGN_LEFT);
	  		clienteNome.setVerticalAlignment(Element.ALIGN_TOP);
	  		clienteNome.setBorder(0);
	  		content.addCell(clienteNome);
  		
			doc.add(content);
			doc.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        
		return null;
	}
	
	public void filtrarOfertaDisciplina(){
		try {
			DateFormat formatter = new SimpleDateFormat("yyyy");  
			Date date = (Date)formatter.parse(buscaAno);  
			ofertaList = (ArrayList<OfertaDisciplina>) relatorioRN.listarOferta(idDisciplina, idProfessor, buscaSemestre, date);
		} catch (RNException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("AQUI!!" + e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
	
	public String selecionarOfertaDisciplina(){
		diaHoje = new Date();
		Long id = ofertaInstanceTemp.getId();
		if (id!=null){
			ofertaInstance = ofertaInstanceTemp;
			try {
				provaList = relatorioRN.listarProvasOferta(ofertaInstance.getId());
			} catch (RNException e) {
				e.printStackTrace();
			}
			ofertaInstanceTemp = new OfertaDisciplina();
			ofertaList = new ArrayList<OfertaDisciplina>();
		} else {
			MsgUtil.msgInfo("Nenhuma oferta selecionada!");
		}
		return null;
	}
	
	public OfertaDisciplina getOfertaInstance() {
		return ofertaInstance;
	}

	public void setOfertaInstance(OfertaDisciplina ofertaInstance) {
		this.ofertaInstance = ofertaInstance;
	}

	public OfertaDisciplina getOfertaInstanceTemp() {
		return ofertaInstanceTemp;
	}

	public void setOfertaInstanceTemp(OfertaDisciplina ofertaInstanceTemp) {
		this.ofertaInstanceTemp = ofertaInstanceTemp;
	}

	public List<OfertaDisciplina> getOfertaList() {
		return ofertaList;
	}

	public void setOfertaList(List<OfertaDisciplina> ofertaList) {
		this.ofertaList = ofertaList;
	}

	public List<AlunoOferta> getAlunoOfertaList() {
		return alunoOfertaList;
	}

	public void setAlunoOfertaList(List<AlunoOferta> alunoOfertaList) {
		this.alunoOfertaList = alunoOfertaList;
	}

	public List<Disciplina> getDisciplinaList() {
		return disciplinaList;
	}

	public void setDisciplinaList(List<Disciplina> disciplinaList) {
		this.disciplinaList = disciplinaList;
	}

	public Long getIdDisciplina() {
		return idDisciplina;
	}

	public void setIdDisciplina(Long idDisciplina) {
		this.idDisciplina = idDisciplina;
	}

	public List<Professor> getProfessorList() {
		return professorList;
	}

	public void setProfessorList(List<Professor> professorList) {
		this.professorList = professorList;
	}

	public Long getIdProfessor() {
		return idProfessor;
	}

	public void setIdProfessor(Long idProfessor) {
		this.idProfessor = idProfessor;
	}

	public String getBuscaSemestre() {
		return buscaSemestre;
	}

	public void setBuscaSemestre(String buscaSemestre) {
		this.buscaSemestre = buscaSemestre;
	}

	public String getBuscaAno() {
		return buscaAno;
	}

	public void setBuscaAno(String buscaAno) {
		this.buscaAno = buscaAno;
	}

	public Prova getProvaInstance() {
		return provaInstance;
	}

	public void setProvaInstance(Prova provaInstance) {
		this.provaInstance = provaInstance;
	}

	public List<Prova> getProvaList() {
		return provaList;
	}

	public void setProvaList(List<Prova> provaList) {
		this.provaList = provaList;
	}

	public Date getDiaHoje() {
		return diaHoje;
	}

	public void setDiaHoje(Date diaHoje) {
		this.diaHoje = diaHoje;
	}

	public List<SelectItem> getAnosList() {
		anosList = new ArrayList<SelectItem>();
		Integer i = 2010;
		for (i=2010; i<2100; i++){
			anosList.add(new SelectItem(i, i.toString()));
		}
		return anosList;
	}
	public void setAnosList(List<SelectItem> anosList) {
		this.anosList = anosList;
	}
	public String getIdentificadorOferta(){
		if (ofertaInstance.getId() != null){
			String str = "";
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
			str = ofertaInstance.getDisciplina().getNome() + " - " + formatter.format(ofertaInstance.getAno()).toString() + "\\" + ofertaInstance.getSemestre(); 
			return str;
		}
		return "";
	}
	
}
